# -*- encoding:utf-8 -*-
# __author__ = 'chenyongbing'
# __poject__ = 'ops_platform'
# __datetime__ = '2022/6/16'
# __name__ = 'tpad_helper.py'
import requests
from urllib.parse import urljoin, urlencode
import json
# Create your code here.


class TapdHelper():
    def __init__(self, base=None, token=None, company_id=None, **kwargs):
        self.base = base
        self.token = token
        self.company_id = company_id

    def _get(self, path, **params):
        url = urljoin(self.base, path) + '?' + urlencode(params, doseq=True)
        response = requests.get(url,  headers={'Authorization': "Basic {}".format(self.token)})

        return response.json()


    def _post(self, path ,data ):
        url = urljoin(self.base, path)
        response = requests.post(url, data, headers={'Authorization': "Basic {}".format(self.token)})


    def workspaces(self):
        path = '/workspaces/projects'
        params = {'company_id': self.company_id}
        return self._get(path, **params)

    def stories(self, **params):
        path = '/stories'
        return self._get(path, **params)

    def bugs(self, **params):
        path = '/bugs'
        return self._get(path, **params)

    def tcases(self, **params):
        path = '/tcases'
        return self._get(path, **params)

    def tplans(self, **params):
        path = '/test_plans'
        return self._get(path, **params)

    def tplan_tcases(self, **params):
        path = '/test_plans/get_test_plan_tcase'
        return self._get(path, **params)

if __name__ == '__main__':
    base = 'https://api.tapd.cn'
    token = 'cTlxQThUTm06MTFGODVGMTgtRDRBMS1ENzEwLTA1MzYtREQ1QzQzQTdEMTIy'
    company_id = '33204822'

    tapd = TapdHelper(base=base, token=token, company_id=company_id)
    # data = tapd.workspaces()
    # print(data)
    data = tapd.bugs(workspace_id='49717773', custom_field_four='2022-07-07')
    with open('bugs.json', 'w') as fr:
        fr.write(json.dumps(data, indent=4, ensure_ascii=False))